package org.no_ip.martin_springwald.google.api.extension.client;

import org.no_ip.martin_springwald.google.api.extension.docs.DocumentsListImportExportFormat;
import org.no_ip.martin_springwald.google.api.extension.docs.DocumentsListMetadataEntry;
import org.no_ip.martin_springwald.google.api.extension.docs.DocumentsListMetadataEntryCallback;
import org.no_ip.martin_springwald.google.api.extension.docs.DocumentsListQuery;
import org.no_ip.martin_springwald.google.api.extension.docs.DocumentsService;
import org.no_ip.martin_springwald.google.api.extension.docs.DocumentsListEntry;
import org.no_ip.martin_springwald.google.api.extension.docs.DocumentsListFeed;
import org.no_ip.martin_springwald.google.api.extension.docs.DocumentsListFeedCallback;
import com.google.api.gwt.client.OAuth2Login;
import com.google.gwt.core.client.Callback;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.gdata.client.GData;
import com.google.gwt.gdata.client.impl.CallErrorException;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextArea;

/**
 * Google API Extension Documents List Sample
 * 
 * @.dependencies gwt-ajaxloader, gwt-gdata, gwt-google-apis, GWT SDK 2.4
 * @.runtimerequirements enabled JavaScript, unblocked Pop-Ups
 * @author Martin Springwald (martin_springwald at web.de)
 * @license MIT (see LICENSE.TXT)
 * @version 1.0.2 public
 */
public class Google_API_Extension implements EntryPoint {
	
	/** Google API Parameter: <br/> API Parameters (Client ID, Application Name) */
	private class GoogleAPIParameter {
		public static final String clientId = "517515512639.apps.googleusercontent.com"; //$NON-NLS-1$
		public static final String appName = "Google API Extension Sample App"; //$NON-NLS-1$
	}

	/** documentsList: <br/> Global DocumentsList Object (initialized after loading GData JS library) */
	private DocumentsService documentsList = null;

	/** onModuleLoad: <br/> Entry point, loads GData JS library (NOTE: personalized API key no longer needed) and proceeds to authorization */
	public void onModuleLoad() { if (!GData.isLoaded()) { GData.loadGDataApi("gdata", new Runnable() { public void run() { login(); } }); } else { login(); } } //$NON-NLS-1$
	
	/** login: <br/> OAuth2 Authorization (sets scope to DocumentsList API) and proceeds to main part */
	private void login() {
		OAuth2Login.get().authorize(
			GoogleAPIParameter.clientId,
	        DocumentsService.DocumentListAuthScope.DOCS,
	        new Callback<Void, Exception>() {
		   		@Override public void onSuccess(Void unused) { show(); }
				@Override public void onFailure(Exception reason) { GWT.log(reason.getMessage()); }
			}
		);
	}
	
	/** show: <br/> Initialize Documents List API and make a sample request */
	private void show() {

		// UI
		RootPanel rootPanel = RootPanel.get("mainContent"); //$NON-NLS-1$
		final TextArea entriesList = new TextArea();
		entriesList.setVisibleLines(10);
		entriesList.setCharacterWidth(200);
		entriesList.setText("Available Documents:\n"); //$NON-NLS-1$
		rootPanel.add(entriesList);
		
		// Documents List API request
		documentsList = DocumentsService.newInstance(GoogleAPIParameter.appName);
    	documentsList.useOAuth2();

    	// List Query
    	documentsList.getFeed(new DocumentsListQuery(), new DocumentsListFeedCallback() {
			@Override public void onFailure(CallErrorException caught) { GWT.log(caught.getMessage()); }
			@Override public void onSuccess(DocumentsListFeed result) {
				DocumentsListEntry[] entries = result.getEntries();
				for (int i = 0; i < entries.length; i++) {
					String entry = "Document ID: " + entries[i].getId().getValue() + "\n" + //$NON-NLS-1$ //$NON-NLS-2$
							"\tTitle: " + entries[i].getTitle().getText() + "\n" + //$NON-NLS-1$ //$NON-NLS-2$
							"\tLast modified by: " + entries[i].getLastModifiedByEmail() + "\n" + //$NON-NLS-1$ //$NON-NLS-2$
							"\tLast viewed: " + entries[i].getLastViewed() + "\n" + //$NON-NLS-1$ //$NON-NLS-2$
							"\tACL Link: " + entries[i].getAclLink() + "\n"; //$NON-NLS-1$ //$NON-NLS-2$
					entriesList.setText(entriesList.getText() + entry);
				}
			}
		});

    	// Metadata Query
    	documentsList.getMetadataEntry(new DocumentsListQuery(), new DocumentsListMetadataEntryCallback() {
			@Override public void onFailure(CallErrorException caught) { GWT.log(caught.getMessage()); }
			@Override public void onSuccess(DocumentsListMetadataEntry result) {
				DocumentsListImportExportFormat[] imports = result.getImportFormats();
				DocumentsListImportExportFormat[] exports = result.getExportFormats();
				entriesList.setText(entriesList.getText() + "\nImport formats:\n"); //$NON-NLS-1$
				for (int i = 0; i < imports.length; i++) {
					String entry = "\t" + imports[i].getSource() + " to " + imports[i].getTarget() + "\n"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
					entriesList.setText(entriesList.getText() + entry);
				}
				entriesList.setText(entriesList.getText() + "\nExport formats:\n"); //$NON-NLS-1$
				for (int i = 0; i < exports.length; i++) {
					String entry = "\t" + exports[i].getSource() + " to " + exports[i].getTarget() + "\n"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
					entriesList.setText(entriesList.getText() + entry);
				}
			}
		});
    	
	}
	
}